using System.Collections.Generic;
using Light.Assets.Scripts.EventManager;
using Refresh;
using UnityEngine;

namespace Light
{
    public class GoodsController_Event : MonoBehaviour
    {
        /// <summary>
        ///     ����Ӧpanel�Ļ�Ծ��
        /// </summary>
        private bool activeOfPanel;

        private BasePanel panel; //GoodsPanel

        /// <summary>
        ///     �ȴ�ˢ�µ�����
        /// </summary>
        private List<StatusBarInfo> waitToRefreshData;

        public BasePanel Panel
        {
            get
            {
                if (panel == null) panel = UIManager.Instance.GetPanel<StatusBarPanel>("StatusBarPanel").goodsPanel;
                return panel;
            }
            set { }
        }

        private void Awake()
        {
            waitToRefreshData = new List<StatusBarInfo>(); //����ռ�

            EventManager.Register<GoodsPanelReOpenEvent>(OnGoodsPanelReOpenEventHandle); //GoodsPanel���´�

            EventManager.Register<StatusBarEvent>(OnStatusBarEventTrigger);

            EventManager.Register<GoodsBarSwtichItemEvent>(OnGoodsBarSwitchItemEventHandle);

            EventManager.Register<GoodsBarDeleteItemEvent>(OnGoodsBarDeleteItemEventHandle);

            EventManager.Register<PickItemsToBackPack>(OnPcikItemsToBackPackEventHandle);
        }

        private void OnGoodsBarSwitchItemEventHandle(GoodsBarSwtichItemEvent @event)
        {
            //ֱ�ӽ������ݽ���������Ҫ����ʵ��������ж�UI���в���
            ItemManager.MoveJsonData(@event.originType, @event.targetType, @event.item);

            //�����ﴦ����json���ݺ󣬽�����QuickSlotBarȥ���������ķ���
            if (@event.targetType == FunctionBarContentType.GoodsItemEuqip)
            {
                using var itemEquipEvt = ItemEquipEvent.Get();
                itemEquipEvt.item = @event.item;
                EventManager.SendEvent(itemEquipEvt);
            }
            else if (@event.targetType == FunctionBarContentType.GoodsScrollView)
            {
                using var itemRelieveEvt = ItemRelieveEvent.Get();
                itemRelieveEvt.item = @event.item;
                EventManager.SendEvent(itemRelieveEvt);
            }

            var info = StatusBarInfo.Get();
            info.type = FunctionBarType.Goods;
            info.contentType = @event.targetType;
            info.operations.Add((new GoodsBarData { data = @event.item, type = @event.item.type },
                FunctionBarOperationType.Add));

            RequestToRefreshPanel(info);

            //@event.Dispose();
        }

        /// <summary>
        ///     ɾ������
        /// </summary>
        /// <param name="event"></param>
        private void OnGoodsBarDeleteItemEventHandle(GoodsBarDeleteItemEvent @event)
        {
            ItemManager.DeleteItem(int.Parse(@event.item.id));

            using var evt = ItemRelieveEvent.Get();
            evt.item = @event.item;
            EventManager.SendEvent(evt);
        }

        private void OnPcikItemsToBackPackEventHandle(PickItemsToBackPack @event)
        {
            //���̨json��������
            ItemManager.AddItemListToJsonDataByItems(@event.items);

            var info = StatusBarInfo.Get();
            info.type = FunctionBarType.Goods;
            foreach (var item in @event.items)
                info.operations.Add((new GoodsBarData { data = item, type = item.type }, FunctionBarOperationType.Add));

            RequestToRefreshPanel(info);
        }

        /// <summary>
        ///     ����Panel���´򿪣�ִ��list��û��ִ�е�info
        /// </summary>
        /// <param name="event"></param>
        private void OnGoodsPanelReOpenEventHandle(GoodsPanelReOpenEvent @event)
        {
            if (waitToRefreshData.Count == 0) return;


            foreach (var item in waitToRefreshData) Panel.Refresh(item);

            waitToRefreshData.Clear();
        }

        private void RequestToRefreshPanel(StatusBarInfo info)
        {
            if (Panel.isActiveAndEnabled)
                Panel.Refresh(info);
            else
                waitToRefreshData.Add(info);
        }

        #region StatusBarEvent

        private void OnStatusBarEventTrigger(StatusBarEvent @event)
        {
            switch (@event.BarType)
            {
                case FunctionBarType.Goods:
                    OnGoodsFunctionBarTrigger(@event);
                    break;
            }

            //@event.Dispose();
        }

        private void OnGoodsFunctionBarTrigger(StatusBarEvent @event)
        {
            switch (@event.BarOperationType)
            {
                case FunctionBarOperationType.Init:
                    GoodsFunctionBarInit(@event);
                    break;
            }
        }

        private void GoodsFunctionBarInit(StatusBarEvent @event)
        {
            @event.contentTypes.ForEach(type =>
            {
                if (type == FunctionBarContentType.GoodsScrollView) GoodsItemsListInit();
                if (type == FunctionBarContentType.GoodsItemEuqip) GoodsItemsEquipInit();
            });
        }

        private void GoodsItemsListInit()
        {
            var info = StatusBarInfo.Get();

            //���ó�ʼ����
            //TODO���һ����������
            info.type = FunctionBarType.Goods;
            info.contentType = FunctionBarContentType.GoodsScrollView;

            foreach (var item in ItemManager.GetGoodsItemsData())
                info.operations.Add((new GoodsBarData { data = item, type = item.type }, FunctionBarOperationType.Add));
            RequestToRefreshPanel(info);
        }

        private void GoodsItemsEquipInit()
        {
            var info = StatusBarInfo.Get();

            //���ó�ʼ����
            //TODO���һ����������
            info.type = FunctionBarType.Goods;

            info.contentType = FunctionBarContentType.GoodsItemEuqip;

            foreach (var item in ItemManager.GetGoodItemsEquipData()) //���Ѿ�װ������Ʒjson�����л�ȡ����
                info.operations.Add((new GoodsBarData { data = item, type = item.type }, FunctionBarOperationType.Add));
            RequestToRefreshPanel(info);
        }

        #endregion StatusBarEvent
    }
}